<template>
    <Form ref="formValidate" class="exform" :model="formValidate" :label-width="100" :rules="ruleValidate">

		<FormItem label="工作站名称" prop="workstationname.zh">
			<Input v-model="formValidate.workstationname.zh" placeholder="工作站名称">
			</Input>
		</FormItem>

		<FormItem label="研究方向" prop="researche.zh" >
			<Input v-model="formValidate.researche.zh" placeholder="研究方向">
			</Input>
		</FormItem>

		<FormItem label="年限" prop="duration.zh" >
			<Input v-model="formValidate.duration.zh" placeholder="年限">
			</Input>
		</FormItem>


		<FormItem label="进站时间" prop="startdate" >
			<Col span="12">
			<DatePicker placement="right" v-model="formValidate.startdate" type="month"  placeholder="请选择进站时间" style="width: 200px" transfer></DatePicker>
			</Col>
		</FormItem>

		<FormItem label="出站时间" prop="enddate" >
			<Col span="12">
			<DatePicker placement="right" v-model="formValidate.enddate" type="month"  placeholder="请选择出站时间" style="width: 200px" transfer></DatePicker>
			</Col>
		</FormItem>

		<div style="width:90%;">
			<FormItem style="width:100%;">
			<Button type="primary" @click="handleSubmit('formValidate')">保存</Button>
			</FormItem>
		</div>
		<div style="clear:both"></div>
		<Spin size="large" fix v-if="spinShow"></Spin>
    </Form>
</template>

<script>

  export default {

    props: {
        teacher_id: {
            type: [Number,String],
            default: null
        },
        infoId: {
            type: [Boolean,String,Number],
            default: false
        }
    },
    data () {
      	return {
            infoUrl:`api/teacher/v1/lebsqualification/info/${this.infoId}`,//请求单条数据
            saveUrl:'',//保存接口
            saveType:null,//保存方式
            spinShow:false,//加载动画
			infoOld:null,

			formValidate: {
				teacher_id:this.teacher_id,
				startdate:'',     //开始时间
				enddate:'',     //结束时间
				shenhe:1,       //审核
          		type:2,
				researche:{
					zh:'',
					mn:'',
					en:'',
				},    //研究方向
				duration:{
					zh:'',
					mn:'',
					en:'',
				},    //年限
				workstationname:{
					zh:'',
					mn:'',
					en:'',
				},    //工作站名称
			},
			ruleValidate: {
				// 开始时间
				startdate: [
					{ required: true, type: 'date', message: '请选择开始时间', trigger: 'blur' }
				],
				// 结束时间
				enddate: [
					{ required: true, type: 'date', message: '请选择结束时间', trigger: 'blur' }
				],
				//研究方向
				'researche.zh': [
					{ required: true, message: '请填写研究方向', trigger: 'blur' }
				],

				//年限
				'duration.zh': [
					{ required: true, message: '请填写年限', trigger: 'blur' }
				],
				//工作站名称
				'workstationname.zh': [
					{ required: true, message: '请填写工作站名称', trigger: 'blur' }
				],

			}, 
      	}
    },
    methods: {
     
        /************************************************************
         * 
         * 单条数据请求
         * 
         ************************************************************/
        infoData(){
            this.spinShow=true//输入框禁用 
			this.$http.get(this.infoUrl).then(val=>{
				this.infoOld = JSON.parse(JSON.stringify(val.data))
				this.formValidate.shenhe = parseInt(this.infoOld.shenhe)//用于默认选的 审核
				
				this.formValidate.startdate = this.infoOld.startdate// 开始时间
				this.formValidate.enddate = this.infoOld.enddate// 结束时间
				this.formValidate.researche = this.infoOld.researche// 研究方向
				this.formValidate.duration = this.infoOld.duration// 年限
				this.formValidate.workstationname = this.infoOld.workstationname// 工作站名称

				this.formValidate.school_id=this.infoOld.school_id;//学校
				this.formValidate.teacher_id=this.infoOld.teacher_id;//教师 
            	this.spinShow=false//输入框禁用   
			})
			.catch(err => {
				if(err.code && err.code==422) {
					this.$Message.error(err.message);
				}
            	this.spinShow=false//输入框禁用   
			});
		},
		
        /************************************************************
         * 
         * 提交时
         * 
         ************************************************************/
      	handleSubmit (name) {
            this.$refs[name].validate((valid) => {
                if (valid) {       
                    this.spinShow=true//输入框禁用   
                    this.saveType(this.saveUrl,this.formValidate).then(val=>{
                        this.$emit('showParent',false)
                        this.$emit('backList',true)
                        this.$Message.success(val.message);
                        this.spinShow=false
                    }).catch(err => {
                        if (err.code && err.code == 422) {
                            this.$Message.error(err.message);
                        }
                        this.spinShow=false
                    })  
                } else {
                    this.$Message.error('表单验证失败!');
                }
            })
      	},
    },
    created() {
		
        if(this.infoId==false){
            // add
            this.UpImgShow=true
            this.saveUrl='api/teacher/v1/lebsqualification/add'
            this.saveType=this.$http.post

        }else{
            // edit
            this.infoData();
            this.saveUrl=`api/teacher/v1/lebsqualification/edit/${this.infoId}`
            this.saveType=this.$http.post
        }
    },
  }
</script>
<style lang="less">
@import'./../public.less';
</style>
